var kms = require("./libs/kmsv21");
var kmsBridge = require("./libs/kmsBridge");
var db = require('./libs/db');
var mqclient = require('./libs/mqclient');
var kmsModel = require('./libs/kmsModelSvr');

var web = require('./libs/webSvr');
var path = require('path');

var sysConfig = {
    db: {
        cock: {
            host: "meeting.xmgkfw.com",
            port: 31000,
            uid: "root",
            pwd: "hhuc1115",
            db: "kms202101",
            sqlPath: path.join(__dirname, "datas/sqls")
        },
        redirection: {
            host: "dq.hhdata.cn",
            port: 31000,
            uid: "root",
            pwd: "hhuc1115",
            db: "redirection_v2022_01",
            sqlPath: path.join(__dirname, "datas/sqls")
        }
    },
    web: {
        port: 31021,
        domain: "meeting.xmgkfw.com",
        https: {
            enable: true,
            keyFn: path.join(__dirname, "datas/keys/7219068_meeting.xmgkfw.com.key"),
            certFn: path.join(__dirname, "datas/keys/7219068_meeting.xmgkfw.com.pem")
        },
        statics: {
            '/': path.join(__dirname, 'datas/static')
        }
    },
    kms: {
        clientId: ['c', (new Date()).getTime(), parseInt(100 * Math.random())].join(""),
        version: "kms.v21",
        recorderPath: "/data/rec",
        kmsUrl: "wss://meeting.xmgkfw.com:31003/kurento",
        clientKmsUrl: "wss://meeting.xmgkfw.com:31003/kurento",
        //kmsUrl: "wss://dq.hhdata.cn:31003/kurento",
        //clientKmsUrl: "wss://dq.hhdata.cn:31003/kurento",
        peerOptions: {
            configuration: {
                "iceServers": [{
                            url: "stun:211.95.25.189:3478"
                        },
                        {
                            urls: ["turn:211.95.25.189:3478"],
                            username: "kurento",
                            credential: "kurento"
                        }
                    ]
                    /* 
                    "iceServers": [{
                            urls: "stun:39.171.39.247:3478"
                        },
                        {
                            urls: ["turn:39.171.39.247:3478"],
                            username: "kurento",
                            credential: "kurento"
                        }
                    ]
                            */
            },
            mediaConstraints: {
                audio: true,
                video: {
                    width: { min: 1280, ideal: 1920, max: 1920 },
                    height: { min: 720, ideal: 1080, max: 1080 }
                },
            }
        }
    },
    kmsBridge: {
        clientId: ['c', (new Date()).getTime(), parseInt(100 * Math.random())].join(""),
        version: "kms.v21",
        recorderPath: "/data/rec",
        kmsUrl: "wss://meeting.xmgkfw.com:31003/kurento",
        clientKmsUrl: "wss://meeting.xmgkfw.com:31003/kurento",
        //kmsUrl: "wss://dq.hhdata.cn:31003/kurento",
        //clientKmsUrl: "wss://dq.hhdata.cn:31003/kurento",
        peerOptions: {
            configuration: {
                "iceServers": [{
                            url: "stun:211.95.25.189:3478"
                        },
                        {
                            urls: ["turn:211.95.25.189:3478"],
                            username: "kurento",
                            credential: "kurento"
                        }
                    ]
                    /* 
                    "iceServers": [{
                            urls: "stun:39.171.39.247:3478"
                        },
                        {
                            urls: ["turn:39.171.39.247:3478"],
                            username: "kurento",
                            credential: "kurento"
                        }
                    ]
                            */
            },
            mediaConstraints: {
                audio: true,
                video: {
                    width: { min: 1280, ideal: 1920, max: 1920 },
                    height: { min: 720, ideal: 1080, max: 1080 }
                },
            }
        }
    },
    kmsModel: {
        clientId: ['c', (new Date()).getTime(), parseInt(100 * Math.random())].join(""),
        version: "kms.v21",
        recorderPath: "/data/rec",
        kmsUrl: "wss://meeting.xmgkfw.com:31003/kurento",
        clientKmsUrl: "wss://meeting.xmgkfw.com:31003/kurento",
        //kmsUrl: "wss://dq.hhdata.cn:31003/kurento",
        //clientKmsUrl: "wss://dq.hhdata.cn:31003/kurento",
        peerOptions: {
            configuration: {
                "iceServers": [{
                            url: "stun:211.95.25.189:3478"
                        },
                        {
                            urls: ["turn:211.95.25.189:3478"],
                            username: "kurento",
                            credential: "kurento"
                        }
                    ]
                    /* 
                    "iceServers": [{
                            urls: "stun:39.171.39.247:3478"
                        },
                        {
                            urls: ["turn:39.171.39.247:3478"],
                            username: "kurento",
                            credential: "kurento"
                        }
                    ]
                            */
            },
            mediaConstraints: {
                audio: true,
                video: {
                    width: { min: 1280, ideal: 1920, max: 1920 },
                    height: { min: 720, ideal: 1080, max: 1080 }
                },
            }
        }
    },
    mqclient: {
        mqClientId: ['mqid', ((new Date()).getTime()).toString(), parseInt(1000 * Math.random()).toString()].join(''),
        mqUrl: "tcp://hhuciot.mqtt.iot.bj.baidubce.com:1883",
        mqUid: "hhuciot/mcu",
        mqPwd: "9vG6KT6V64S2uS3P"
    }
};
var server = {
    config: sysConfig,
    kms: new kms.TKms(),
    kmsBridge: new kmsBridge.TKmsBridge(),
    kmsModel: new kmsModel.TKmsModelSvr(),
    db: new db.TDBMySql(),
    mqclient: new mqclient.TMqClient(),
    web: new web.TWebSvr(),
    initConfig() {
        var me = this;
        return new Promise((success, faild) => {
            try {
                var rootPath = __dirname;
                //web settings
                me.web.config = me.config.web;
                //db settings 
                Object.keys(me.config.db).forEach(key => {
                    var item = me.config.db[key];
                    item.sqlPath = item.sqlPath;
                    me.db.setConfig(key, item);
                    me.db.regist2WebSvr(me.web);
                });
                //kms settings
                me.kms.config = me.config.kms;
                me.kms.regist2WebSvr(me.web);
                //kmsBridge settings
                me.kmsBridge.config = me.config.kmsBridge;
                me.kmsBridge.regist2WebSvr(me.web);
                me.kmsBridge.getClient().then(_ => {
                    console.log('connect to kmsbridge.')
                });
                //kmsModel settings
                me.kmsModel.config = me.config.kmsModel;
                me.kmsModel.regist2WebSvr(me.web);
                me.kmsModel.start().then(_ => {
                    console.log('kms model server started.')
                });
                // mq settings
                me.mqclient.config = me.config.mqclient;
                success();
            } catch (er) { console.log(er) };
        });
    },
    start() {
        var me = this;
        return new Promise((success, faild) => {
            try {
                me.initConfig().then(_ => {
                    return me.kms.getClient();
                }).then(_ => {
                    console.log(me.kms.kmsClient.svr.id);
                    return me.web.start();
                }).then(_ => {
                    success();
                })
            } catch (er) { console.log(er) };
        });
    }
};

server.start().then(_ => {
    console.log('start km service...');
})